<?php

require_once("perso.class.php");
error_reporting(0);

define("ONE_DAY", 86400);

$id = $_GET['id'];
$file = "images/player_" . $id . ".png";

$flag = 0;
$next_time_update = 1;
$current_time = 0;

if (file_exists($file))
  {
    $next_time_update = filemtime($file) + ONE_DAY;
    $current_time = time();
  }
else
  $flag = 1;
if ($next_time_update <= $current_time || $flag)
  {
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, "http://ladder.dofus.com/proxy.html");
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, "id=" . $id);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	$data = json_decode(curl_exec($ch));
        if (isset($data[0]))
	{
	    $perso = new Perso($data[0]);
	    $perso->makeImage();
	}
  }

header("Content-Type: image/png");
imagepng(imagecreatefrompng($file));
